package ui;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import model.TransportDetail;
import utils.ExcelExportUtil;
import utils.ExcelImportUtil;
import utils.ExcelLoadUtil;

public class TransportListPage {
    private Stage stage;

    public TransportListPage(Stage stage) {
        this.stage = stage;
    }

    public void show() {
        new ComingSoonPage().show(stage);
    }

    private void setupButton(Button button, String styleClass) {
        button.getStyleClass().add(styleClass);
        button.setStyle("-fx-font-size: 16px; -fx-padding: 10 30;");
        button.setPrefWidth(150);
        button.setPrefHeight(45);
    }

    private void showAddDialog() {
        // 实现添加对话框
        Dialog<TransportDetail> dialog = new Dialog<>();
        dialog.setTitle("添加运输明细");
        dialog.setHeaderText("请输入运输明细信息");

        // 设置按钮类型
        ButtonType addButtonType = new ButtonType("添加", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(addButtonType, ButtonType.CANCEL);

        // 创建表单
        VBox form = new VBox(10);
        form.setPadding(new Insets(20));

        TextField locationField = new TextField();
        locationField.setPromptText("地点");
        TextField weightField = new TextField();
        weightField.setPromptText("重量(吨)");

        form.getChildren().addAll(
                new Label("地点:"), locationField,
                new Label("重量(吨):"), weightField
        );

        dialog.getDialogPane().setContent(form);

        // 转换结果为TransportDetail对象
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == addButtonType) {
                TransportDetail detail = new TransportDetail();
                detail.setLogisticsId(locationField.getText());
                detail.setWeight(weightField.getText());
                return detail;
            }
            return null;
        });

        dialog.showAndWait().ifPresent(result -> {
            // 这里可以添加保存逻辑
            showAlert("添加成功", "已成功添加运输明细");
        });
    }

    private void showEditDialog(TransportDetail detail) {
        // 实现编辑对话框
        Dialog<TransportDetail> dialog = new Dialog<>();
        dialog.setTitle("修改运输明细");
        dialog.setHeaderText("修改运输明细信息");

        ButtonType saveButtonType = new ButtonType("保存", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(saveButtonType, ButtonType.CANCEL);

        VBox form = new VBox(10);
        form.setPadding(new Insets(20));

        TextField locationField = new TextField(detail.getLogisticsId());
        TextField weightField = new TextField(detail.getWeight());

        form.getChildren().addAll(
                new Label("地点:"), locationField,
                new Label("重量(吨):"), weightField
        );

        dialog.getDialogPane().setContent(form);

        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == saveButtonType) {
                detail.setLogisticsId(locationField.getText());
                detail.setWeight(weightField.getText());
                return detail;
            }
            return null;
        });



        dialog.showAndWait().ifPresent(result -> {
            showAlert("修改成功", "运输明细已更新");
        });
    }

    private void showDeleteConfirmation(TransportDetail detail) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("确认删除");
        alert.setHeaderText("确定要删除这条运输明细吗？");
        alert.setContentText("地点: " + detail.getLogisticsId() + "\n重量: " + detail.getWeight());

        alert.showAndWait().ifPresent(response -> {
            if (response == ButtonType.OK) {
                // 这里可以添加删除逻辑
                showAlert("删除成功", "运输明细已删除");
            }
        });
    }

    private void showAlert(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
}